﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class LapPhieuDeNghiMuaHang : UserControl
    {
        public LapPhieuDeNghiMuaHang()
        {
            InitializeComponent();
        }

        private void LapPhieuMuaHang1_Load(object sender, EventArgs e)
        {
            loadData();
            button2_Click(null, null);
        }

        private void loadData()
        {
            searchPhieuDeNghi1.refreshGrid();
        }



        private void btnNew_Click(object sender, EventArgs e)
        {
            // new
            OtherForms.ThemPhieuDeNghiMuaHangForm f = new OtherForms.ThemPhieuDeNghiMuaHangForm();
            f.ShowDialog();
            loadData();
            /*
            // Tao quy trinh
            if (f.getSuccess())
            {
                // save
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.QuyTrinhMuaHang quyTrinh = new _DB.QuyTrinhMuaHang();
                quyTrinh.TenQuyTrinh = DateTime.Now.ToString();
                quyTrinh.MaPhieuDeNghiMuaHang = f.getMaPhieu();
                quyTrinh.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_DE_NGHI;
                dc.QuyTrinhMuaHangs.InsertOnSubmit(quyTrinh);
                dc.SubmitChanges();

                
            }*/
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            // check select
            if (dgvPhieuMH.SelectedRows.Count <= 0)
                return;

            // check trang thai
            _DB.QuyTrinhMuaHang qt = (_DB.QuyTrinhMuaHang)dgvPhieuMH.SelectedRows[0].Tag;
            if (qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_DE_NGHI &&
                qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_DE_NGHI &&
                qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_DE_NGHI)
            {
                return;
            }

            // open edit form
            string maPhieu = (string)dgvPhieuMH.SelectedRows[0].Cells[1].Value;
            OtherForms.ThemPhieuDeNghiMuaHangForm f = new OtherForms.ThemPhieuDeNghiMuaHangForm(maPhieu);
            f.ShowDialog();
            loadData();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            // check select
            if (dgvPhieuMH.SelectedRows.Count <= 0)
                return;

            // check trang thai
            _DB.QuyTrinhMuaHang qt = (_DB.QuyTrinhMuaHang)dgvPhieuMH.SelectedRows[0].Tag;
            if (qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_DE_NGHI &&
                qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_DE_NGHI)
            {
                return;
            }

            // confirm
            if (MessageBox.Show("Chắc chắn", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            string maPhieu = (string)dgvPhieuMH.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuDeNghiMuaHang == maPhieu);
            qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_DE_NGHI;
            dc.SubmitChanges();
            loadData();
        }

        private void dgvPhieuMH_DoubleClick(object sender, EventArgs e)
        {
            // check select
            if (dgvPhieuMH.SelectedRows.Count <= 0)
                return;
            
            // open form
            string maPhieu = (string)dgvPhieuMH.SelectedRows[0].Cells[1].Value;
            OtherForms.XemPhieuDeNghiMuaHangForm f = new OtherForms.XemPhieuDeNghiMuaHangForm(maPhieu);
            f.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // check select
            if (dgvPhieuMH.SelectedRows.Count <= 0)
                return;

            // check trang thai
            _DB.QuyTrinhMuaHang qt = (_DB.QuyTrinhMuaHang)dgvPhieuMH.SelectedRows[0].Tag;
            if (qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHINH_SUA_PHIEU_DE_NGHI &&
                qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.CHO_DUYET_PHIEU_DE_NGHI &&
                qt.TrangThai != _DB.TrangThaiQuyTrinhMuaHang.KHONG_DUYET_PHIEU_DE_NGHI)
            {
                return;
            }

            // confirm
            if (MessageBox.Show("Chắc chắn", "", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            // go
            string maPhieu = (string)dgvPhieuMH.SelectedRows[0].Cells[1].Value;
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            qt = dc.QuyTrinhMuaHangs.Single(x => x.MaPhieuDeNghiMuaHang == maPhieu);
            qt.PhieuDeNghiMuaHang.deleted = true;
            qt.deleted = true;
            dc.SubmitChanges();
            loadData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            splitContainer1.Panel1Collapsed = !splitContainer1.Panel1Collapsed;
        }

        private void btnReport_Click(object sender, EventArgs e)
        {
            if (dgvPhieuMH.SelectedRows.Count == 0)
                return;
            string maPhieu = (string)dgvPhieuMH.SelectedRows[0].Cells[1].Value;
            //MessageBox.Show(maPhieu);
            Reports._DS_PhieuDeNghiMuaHang ds = new Reports._DS_PhieuDeNghiMuaHang();
            ds.fillData(maPhieu);
            ds.viewReport();
            
        }


    }
}
